package cn.biplam.setup.code;

import java.util.ArrayList;

import cn.biplam.back.sql.dml.Delete;
import cn.biplam.back.sql.dml.Insert;
import cn.biplam.back.sql.dml.Update;
import cn.biplam.common.client.DataFilter;
import cn.biplam.common.tools.TOOLS;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.sqlresult.SqlResult;
import cn.biplam.setup.code.product.ProductData;
import cn.biplam.setup.code.product.ProductData.ProductStaus;

/**
 * 交换代码管理-交换代码数据操作
 * @author XiaoRuxing Sep 22, 20096:10:20 PM
 * @version 1.0
 */
public class ExchangeCodeData extends Data{

	public String CODE_ID ;
	public String CODE_NAME ;
	public String CODE_CLASS_PATH ;
	public String CODE_TYPE ;
	public String CODE_DESC ;
	public String CODE_ORG_CLASS_PATH ;
	public String PRODUCT_ID;
	public String PUBLISH;


	protected String tableName;

	/**
	 * 交换代码类型
	 */
	public enum Code_Type{
		/** ajx交换代码 */
		Ajx_Code,
		/** operator交换代码 */
		Operator_Code,
		/** 页面代码 */
		Page_Code,
		/** bean操作交换码 */
		Bean_Operator_Code;
	}

	/**
	 * 获取指定代码的所有上级代码
	 * @param codeId    代码ID
	 * @return
	 * @throws ErrorMessage
	 */
	public ArrayList<String[]> getParents(String codeId) throws ErrorMessage{
		ArrayList<String[]> codeParents = new ArrayList<String[]>();
		SqlResult sr = getCode(codeId);
		while(TOOLS.exist(sr)){
			String codeName = null;
			String codeParent = null;
			String[] code = null;
			sr.next();
			codeParent = sr.getString(CODE_TYPE);
			codeName = sr.getString(CODE_NAME);
			codeId = sr.getString(CODE_ID);
			code = new String[2];
			code[0] = codeId;
			code[1] = codeName;
			codeParents.add(code);
			codeId = codeParent;
			sr = getCode(codeId);
		}
		return TOOLS.reverseList(codeParents);
	}

	/**
	 *  获取父亲ID
	 * @param codeId 父亲iD
	 * @return 父亲ID
	 * @throws ErrorMessage
	 */
	public String getParent(String codeId,String codeType) throws ErrorMessage{
		SqlResult code = getCode(codeId);
		String parent = null;
		if(TOOLS.exist(code)){
			code.next();
			parent = code.getString(CODE_TYPE);
		}
		if(TOOLS.StringIsEmpty(parent)){
			parent = codeType;
		}
		return parent;
	}
	
	/**
	 * 获取指定代码
	 * @param codeId
	 * @return
	 */
	public SqlResult getCode(String codeId){
		StringBuilder sp = new StringBuilder();
		sp.append(" select * from ");
		sp.append(this.tableName);
		sp.append(" where ");
		sp.append(CODE_ID);
		sp.append("='");
		sp.append(codeId);
		sp.append("'");
		SqlResult sr = this.executeSelectSql(sp.toString());
		return sr;
	}

	/**
	 * 获取产品下代码
	 * @param productId
	 * @return
	 */
	public SqlResult getProductCodes(String productId){
		StringBuilder sp = new StringBuilder();
		sp.append(" select * from ");
		sp.append(this.tableName);
		sp.append(" where ");
		sp.append(PRODUCT_ID);
		sp.append("='");
		sp.append(productId);
		sp.append("'");
		SqlResult sr = this.executeSelectSql(sp.toString());
		return sr;
	}

	/**
	 * 获取指定代码下的所有下级代码
	 * @param codeType 代码类型
	 * @param codeId   代码ID
	 * @return
	 */
	public SqlResult getUnderCodes(String codeId){
		StringBuilder sp = new StringBuilder();
		sp.append(" select * from ");
		sp.append(this.tableName);
		sp.append(" where ");
		sp.append(CODE_TYPE);
		if(!TOOLS.StringIsEmpty(codeId)){
			sp.append("='");
			sp.append(codeId);
			sp.append("'");
		}else{
			sp.append(" is null ");
		}
		sp.append(" order by ");
		sp.append(CODE_ID);
		return executeSelectSql(sp.toString());
	}

	/**
	 * 添加代码
	 * @param filter
	 * @return
	 * @throws ErrorMessage 
	 */
	public boolean AddCode(DataFilter filter) throws ErrorMessage{
		Insert insert = new Insert(this.tableName);
		String codeId = filter.getValue(CODE_ID);
		String codeType = filter.getValue(CODE_TYPE);

		if(TOOLS.StringIsEmpty(codeId)){
			codeId = codeType+getUnderCodesNumb(codeType);
		}

		String classPath = filter.getValue(CODE_CLASS_PATH);
		classPath = TOOLS.replaceBreakLineToNull(classPath);

		insert.putStringFieldDenyNull(CODE_ID, codeId);
		insert.putStringFieldIgnoreNull(CODE_NAME, filter.getValue(CODE_NAME));
		insert.putStringFieldIgnoreNull(CODE_CLASS_PATH, classPath);
		insert.putStringFieldIgnoreNull(CODE_ORG_CLASS_PATH, classPath);
		insert.putStringFieldIgnoreNull(CODE_TYPE, codeType);
		insert.putStringFieldIgnoreNull(CODE_DESC, filter.getValue(CODE_DESC));
		String productId = filter.getValue(PRODUCT_ID);
		String status = null;
		if(!TOOLS.StringIsEmpty(productId)){
			status = new ProductData().getStatus(productId);
			insert.putStringFieldDenyNull(PRODUCT_ID, productId);
		}else{
			status = ProductStaus.Start;
		}
		insert.putStringFieldDenyNull(PUBLISH, status);
		return this.executeDMLSql(insert.getSimpleSql());
	}

	/**
	 * 更新产品下的所有模块状态
	 * @param status    状态
	 * @param productId 产品ID
	 * @return
	 * @throws ErrorMessage
	 */
	public String updateStatus(String status,String productId) throws ErrorMessage{
		Update update = new Update(this.tableName);
		update.putStringFieldIgnoreNull(PUBLISH, status);
		update.setWhere(PRODUCT_ID+"='"+productId+"'");
		return update.getSimpleSql();
	}

	/**
	 * 获取代码ID
	 * @param codeType
	 * @return
	 * @throws ErrorMessage 
	 */
	public String getCodeId(String codeType) throws ErrorMessage{
		if(!Code_Type.Ajx_Code.toString().equals(codeType)
				&&!Code_Type.Operator_Code.toString().equals(codeType)
				&&!Code_Type.Bean_Operator_Code.toString().equals(codeType)
				&&!Code_Type.Page_Code.toString().equals(codeType)){
			return codeType+getUnderCodesNumb(codeType);
		}else{
			return getUnderCodesNumb(codeType);
		}
	}

	/**
	 * 获取所有代码
	 */
	public SqlResult getCodes(){
		StringBuilder sp = new StringBuilder();
		sp.append(" select * from ");
		sp.append(this.tableName);
		SqlResult sr = executeSelectSql(sp.toString());
		return sr;
	}

	/**
	 * 获取指定代码下的所有下级代码
	 * @param codeType 代码类型
	 * @param codeId   代码ID
	 * @return
	 * @throws ErrorMessage 
	 */
	public String getUnderCodesNumb(String codeId) throws ErrorMessage{
		boolean isNumb = (!TOOLS.StringIsEmpty(codeId)) && TOOLS.isNumeric(codeId);
		StringBuilder sp = new StringBuilder();
		sp.append(" select max("+CODE_ID+") from ");
		sp.append(this.tableName);
		sp.append(" where ");
		sp.append(CODE_TYPE);
		if(isNumb){
			sp.append("='");
			sp.append(codeId);
			sp.append("'");
		}else{
			sp.append(" is null ");
		}
		String codes = null;
		SqlResult sr = executeSelectSql(sp.toString());
		if(TOOLS.exist(sr)){
			sr.next();
			codes = sr.getString(0);
		}
		if(codes!=null && codes.length()>0
				&& TOOLS.isNumeric(codes)){
			codes = (TOOLS.stringToBigInt(codes)+1)+"";
			if(isNumb){
				codes = codes.substring(codeId.length(), codes.length());
			}
		}
		if(codes==null){
			codes = "0";
		}
		return codes;
	}

	/**
	 * 修改代码
	 * @param filter
	 * @return
	 * @throws ErrorMessage 
	 */
	public boolean UpdateCode(DataFilter filter) throws ErrorMessage{
		return this.executeDMLSql(getUpdateCodeSql(filter));
	}

	/**
	 * 修改代码
	 * @param filter
	 * @return
	 * @throws ErrorMessage 
	 */
	public String getUpdateCodeSql(DataFilter filter) throws ErrorMessage{
		String codeId = filter.getValue(CODE_ID);
		String classPath = filter.getValue(CODE_CLASS_PATH);
		classPath = TOOLS.replaceBreakLineToNull(classPath);
		Update update = new Update(this.tableName);
		update.putStringFieldIgnoreNull(CODE_NAME, filter.getValue(CODE_NAME));
		update.putStringFieldIgnoreNull(CODE_CLASS_PATH, classPath);
		update.putStringFieldIgnoreNull(CODE_ORG_CLASS_PATH, classPath);
		update.putStringFieldIgnoreNull(CODE_DESC, filter.getValue(CODE_DESC));
		String productId = filter.getValue(PRODUCT_ID);
		String status = null;
		if(!TOOLS.StringIsEmpty(productId)){
			status = new ProductData().getStatus(productId);
			update.putStringFieldIgnoreNull(PRODUCT_ID, productId);
			update.putStringFieldIgnoreNull(PUBLISH, status);
		}
		update.setWhere(CODE_ID+"='"+codeId+"'");
		return update.getSimpleSql();
	}

	/**
	 * 删除代码
	 * @param codeId 代码ID
	 * @return
	 * @throws ErrorMessage 
	 */
	public boolean delCode(String codeId) throws ErrorMessage{
		Delete delete = new Delete(this.tableName);
		delete.setWhere(CODE_ID+"='"+codeId+"'");
		return this.executeDMLSql(delete.getSimpleSql());
	}

	/**
	 * 删除代码(如果是主题则删除主题下所有代码)
	 */
	public void delSubCodes(String codeId) throws ErrorMessage{
		SqlResult sr = this.getUnderCodes(codeId);
		if(TOOLS.exist(sr)){
			while(sr.next()){
				delSubCodes(sr.getString(CODE_ID));
			}
		}
		delCode(codeId);
	}

	/**
	 * 根据查找条件查找
	 * @param filter
	 * @return
	 */
	public SqlResult search(DataFilter filter){
		String codeId = filter.getValue(CODE_ID);
		String codeName = filter.getValue(CODE_NAME);
		String codePath = filter.getValue(CODE_CLASS_PATH);
		StringBuilder sp = new StringBuilder();
		sp.append(" select * from ");
		sp.append(this.tableName);
		sp.append(" where 1=1 ");
		if(!TOOLS.StringIsEmpty(codeId)){
			sp.append(" and ");
			sp.append(CODE_ID);
			sp.append(" like '%"+codeId+"%'");
		}
		if(!TOOLS.StringIsEmpty(codeName)){
			sp.append(" and ");
			sp.append(CODE_NAME);
			sp.append(" like '%"+codeName+"%'");
		}
		if(!TOOLS.StringIsEmpty(codePath)){
			sp.append(" and ");
			sp.append(CODE_CLASS_PATH);
			sp.append(" like '%"+codePath+"%'");
		}
		return this.executeSelectSql(sp.toString());
	}
}
